import {ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {BasicService} from "../../unit/basic-service";
import {ApplicationItem, NewItem} from "../../modals/basic.modal";
import {environment} from "../../../environments/environment";
import {AlertComponent} from "../../unit/alert";

@Component({
    selector: 'app-main',
    templateUrl: './main.page.html',
    styleUrls: ['./main.page.scss'],
})
export class MainPage implements OnInit {
    cityName: string;
    iconList: Array<ApplicationItem> = [];
    themeNewList: Array<NewItem>;
    hotNewList: Array<NewItem>;

    constructor(
        private route: Router,
        private activateInfo: ActivatedRoute,
        private alertComponent: AlertComponent,
        private basicService: BasicService,
        private changeDetectorRef: ChangeDetectorRef
    ) {
    }

    ngOnInit() {
        this.cityName = '成都';
        this.activateInfo.queryParamMap.subscribe((res: any) => {
            this.getApplication();
        });
        this.getNews();
    }

    getApplication() {
        this.iconList = [];
        const idCard = localStorage.getItem('cardNumber');
        this.basicService.getData('index/getFollow', {idCard}).subscribe(res => {
            this.reGroupApplication(res);
        });
    }

    getNews() {
        // 专题
        this.basicService.getData('appMessage/ztxw').subscribe(res => {
            this.reGroupNews('theme', res);
        });
        // 热点
        this.basicService.getData('appMessage/rdxw').subscribe(res => {
            this.reGroupNews('hot', res);
        })
    }

    reGroupNews(type, data) {
        //
        const token = localStorage.getItem('medicalToken'); // token保存在localstorage

        data.map(item => {
            item.url = `${environment.basicUrl}/uploadappmessage/${item.filepath}?access_token=${token}`;
        });
        if (type === 'hot') {
            this.hotNewList = [...data];
        }
        if (type === 'theme') {
            this.themeNewList = [...data];
        }
    }

    reGroupApplication(data) {
        this.iconList = [
            {
                app_name: '更多',
                app_icon: './assets/image/main/a-icon-04.png',
                bg: '#D2A5FF',
                id: '123'
            }
        ];
        const colorList = ['#27E2B6', '#00B6ED', '#00D0DE', '#D2A5FF'];
        const currentListName = [];
        data.map((item, index) => {
            const num = index % 1 ? 1 : index % 2 ? 2 : index % 3 ? 3 : 0;
            item.bg = colorList[num];
            item.app_icon = `./assets/image/main/a-icon-0${num + 1}.png`;
            if (item.sfgz === 1) {
                this.iconList.unshift(item);
                currentListName.push(item.app_name);
            }
        });
        this.changeDetectorRef.markForCheck();
        this.changeDetectorRef.detectChanges();
    }

    toToPage(item) {
        this.route.navigate([`/${item.navigation}`],
            {queryParams: {...item}}).then();
    }

    applicationPage(item) {
        if (item.app_name !== '更多') {
            // alert('更多模块开发中...');
            return;
        } else {
            this.route.navigate([`/application`]).then();
        }
    }

    goToPage(type) {
        this.route.navigate([`/news`],
            {queryParams: {type}}).then();
    }
}
